/* 
    axios二次封装及配置拦截器
 */

import axios from 'axios'
import qs from 'qs' // 引入qs模块，用来序列化post类型的数据，某些请求会用得到
import { ElMessage } from 'element-plus'

const instance = axios.create({
    baseURL: '/api',
    timeout: 5000,
    headers: {
        'Content-Type':'application/json;charset=UTF-8'
    },
})

// 请求拦截
instance.interceptors.request.use(
    config => {
        let token = localStorage.getItem('token')
        if (token) {
            config.headers.Authorization = "Bearer" + " " + token || ""
        }
		if (config.method === 'post' || config.method === 'put') {
			// config.data = qs.stringify({...config.data });
			if (config.headers['Content-Type'] === 'multipart/form-data') {

			} else {
				config.headers['Content-Type'] = 'application/json; charset=utf-8';
				config.data = JSON.stringify({...config.data })
			}
		}
        return config
    },
    error => {
        return Promise.reject(error)
    }
)

// 响应拦截
instance.interceptors.response.use(
    response => {
        let data
        // IE9时response.data是undefined，因此需要使用response.request.responseText(Stringify后的字符串)
        if (response.data == undefined) {
            data = JSON.parse(response.request.responseText)
        } else {
            data = response.data
        }
        return data
    },
    error => {
        // console.log(error.response)
        if (error && error.response) {
            switch (error.response.status) {
                case 400:
                    error.message = '请求错误'
                    break

                case 401:
                    localStorage.removeItem('token')
                    router.replace({
                        path: '/login',
                        query: {
                            redirect: router.currentRoute.fullPath
                        } // 登录成功后跳入当前浏览的页面
                    })
                    break

                case 403:
                    error.message = '拒绝访问'
                    break

                case 404:
                    error.message = `请求地址出错: ${error.response.config.url}`
                    break

                case 408:
                    error.message = '请求超时'
                    break

                case 500:
                    error.message = '服务器内部错误'
                    break

                case 501:
                	error.message = '服务未实现'
                    break

                case 502:
                    error.message = '网关错误'
                    break

                case 503:
                    error.message = '服务不可用'
                    break

                case 504:
                	error.message = '网关超时'
                    break

                case 505:
                    error.message = 'HTTP版本不受支持'
                    break

                default:
            }
        }
        ElMessage.error(`${error}`);
        return Promise.reject(error)
    }
)

export default instance
